﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MapTestCamera : MonoBehaviour
{
    //属性值
    private float moveSleep = 3f;


    // 引用变量

    //精灵渲染器
    private SpriteRenderer spriteRenderer;
    //坦克的朝向数组，此处需要使用 public 才能在GUI中显示
    public Sprite[] tankDirection;

    private void Awake()
    {
        //初始化渲染器
        spriteRenderer = GetComponent<SpriteRenderer>();
    }


    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
    }

    private void FixedUpdate()
    {

        PlayerMove();
    }

    // 坦克移动方法
    private void PlayerMove()
    {
        //按WSAD 或者方向键执行移动
        //垂直上下移动
        float v = Input.GetAxisRaw("Vertical");
        transform.Translate(Vector3.up * v * moveSleep * Time.fixedDeltaTime, Space.World);
        if (v > 0)
        {
            //向上
            spriteRenderer.sprite = tankDirection[0];
        }
        else if (v < 0)
        {
            //向下
            spriteRenderer.sprite = tankDirection[2];
        }
        if (v != 0)
        {
            //禁止坦克斜着走，优先上下移动
            return;
        }

        //水平左右移动
        float h = Input.GetAxisRaw("Horizontal");
        transform.Translate(Vector3.right * h * moveSleep * Time.fixedDeltaTime, Space.World);
        if (h < 0)
        {
            //向左
            spriteRenderer.sprite = tankDirection[3];
        }
        else if (h > 0)
        {
            //向右
            spriteRenderer.sprite = tankDirection[1];
        }

    }


    private void FindAll()
    {
        GameObject[] all = FindObjectsOfType(typeof(GameObject)) as GameObject[];
        foreach (GameObject child in all)
        {
            Debug.Log(child.transform.name);
            Vector3 v3=child.transform.position;
            Debug.Log("x:"+v3.x+"  y:"+v3.y+"  z:"+v3.z);
        }
    }
}
